﻿using System;
using System.IO;

namespace DotNetNuke.Modules.UserDefinedTable
{

  public class CSVWriter
  {



    private static string EncodeString(string str, string delimiter)
    {
      string str3;
      int index = str.IndexOf(delimiter);
      int num3 = str.IndexOf('\r');
      int num2 = str.IndexOf('"');
      if (num2 >= 0)
      {
        str3 = str.Replace("\"", "\"\"");
      }
      else
      {
        str3 = str;
      }
      if (((index >= 0) | (num2 >= 0)) | (num3 >= 0))
      {
        str3 = "\"" + str3 + "\"";
      }
      return str3;
    }




    public static void WriteCSV(string[] data, StreamWriter sw, string delimiter)
    {
      int num2 = data.Length - 1;
      for (int i = 0; i <= num2; i++)
      {
        sw.Write(EncodeString(data[i], delimiter));
        if (i != (data.Length - 1))
        {
          sw.Write(delimiter);
        }
      }
      sw.WriteLine("");
      sw.Flush();
    }



  }

}